define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    //
    function getType() {
        var selected = $('#c-paytype_id').val();
        if (selected == 1) {
            $('#c-paytype_id').parent().parent().hide();
        }

        // $('#c-paytype').on("change", function () {
        //     if ($(this).val() == 1) {
        //         $('#c-card_id_text').parent().parent().parent().hide();
        //     } else {
        //         $('#c-card_id_text').parent().parent().parent().show();
        //     }
        // });
    }


    function getRedirect() {
        var selected = $('#c-redirect_type').val();
        if (selected == 3) {
            $('.redirect_url').hide();
        }

        $('#c-redirect_type').on("change", function () {
            if ($(this).val() == 3) {
                $('.redirect_url').show();
            } else {
                $('.redirect_url').hide();
            }
        });





        var selectedpaytype = $('#c-paytype').val();
        if (selectedpaytype == 2) {
            $('.c-turntable_id').show();
            $('.c-Card_id').hide();
            $('.c-Scan_times').hide();


        }


        $('#c-paytype').on("change", function () {
            if ($(this).val() == 2) {
                $('.c-turntable_id').show();
                $('.c-Card_id').hide();
                $('.c-Scan_times').hide();
    
            } else {
                $('.c-turntable_id').hide();
                $('.c-Card_id').show();
                $('.c-Scan_times').show();
            }
        });



    }


    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'activity/index' + location.search,
                    add_url: 'activity/add',
                    edit_url: 'activity/edit',
                    del_url: 'activity/del',
                    multi_url: 'activity/multi',
                    import_url: 'activity/import',
                    table: 'activity',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'title', title: __('Title'), operate: 'LIKE'},
                        /*{field: 'manystore_id', title: __('Manystore_id')},
                        {field: 'card_id', title: __('Card_id')},*/
                        {field: 'card.title', title: __('Card.title'), operate: 'LIKE'},
                        {field: 'manystoreshop.name', title: __('Manystoreshop.name'), operate: 'LIKE'},
                        {field: 'scan_times', title: __('Scan_times')},
                        {
                            field: 'starttime',
                            title: __('Starttime'),
                            operate: 'RANGE',
                            addclass: 'datetimerange',
                            autocomplete: false,
                            formatter: Table.api.formatter.datetime
                        },
                        {
                            field: 'endtime',
                            title: __('Endtime'),
                            operate: 'RANGE',
                            addclass: 'datetimerange',
                            autocomplete: false,
                            formatter: Table.api.formatter.datetime
                        },
                        {field: '', title: '状态', table: table, formatter: function (value, row, index) {
                                var time=Math.round(new Date().getTime()/1000);
                                if(row.starttime>time)
                                {
                                    var value="未开始";
                                }
                                else if(row.starttime<time<row.endtime)
                                {
                                    var value="进行中";

                                }
                                else if(row.endtime<time)
                                {
                                    var value="已结束";
                                }

                                var custom = {进行中: 'success', 未开始: 'danger', 已结束: 'info'};
                                var color=custom[value];
                                var icon = 'fa fa-circle';

                                return  '<span class="text-' + color + '">' + (icon ? '<i class="' + icon + '"></i> ' : '') + value + '</span>';

                            }},

                        /*  {
                              field: 'image',
                              title: __('Image'),
                              operate: false,
                              events: Table.api.events.image,
                              formatter: Table.api.formatter.image
                          },*/
                        {
                            field: 'paytype',
                            title: __('Paytype'),
                            searchList: {"0": __('Paytype 0'), "1": __('Paytype 1'), "2": __('大转盘')},
                            formatter: Table.api.formatter.normal
                        },
                        {
                            field: 'createtime',
                            title: __('Createtime'),
                            operate: 'RANGE',
                            addclass: 'datetimerange',
                            autocomplete: false,
                            formatter: Table.api.formatter.datetime
                        },

                        {
                            field: 'operate',
                            title: __('Operate'),
                            table: table,
                            events: Table.api.events.operate,
                            buttons:[
                                {
                                    name:"poster",
                                    text:"海报",
                                    icon:"fa fa-share-alt-square",
                                    url:"activity/poster",
                                    classname:"btn btn-xs btn-warning btn-dialog",
                                    //extend:"data-area='[\"100%\",\"100%\"]'"

                                },
                                {
                                    name:"seller",
                                    text:"核销",
                                    icon:"fa fa-check-square",
                                    url:"activity/seller",
                                    classname:"btn btn-xs btn-success btn-dialog",


                                },
                                {
                                    name:"more",
                                    text:"领劵记录",
                                    icon:"fa fa-folder-0",
                                    url:"activity/sellcards",
                                    classname:"btn btn-xs  btn-dialog",
                                    dropdown:"更多"
                                },
                                {
                                    name:"more",
                                    text:"参与用户",
                                    icon:"fa fa-folder-0",
                                    url:"activity/selluser",
                                    classname:"btn btn-xs  btn-dialog",
                                    dropdown:"更多"
                                },
                                {
                                    name:"more",
                                    text:"活动数据",
                                    icon:"fa fa-folder-0",
                                    url:"activity/statistics",
                                    classname:"btn btn-xs  btn-dialog",
                                    dropdown:"更多"
                                },
                            ],
                            formatter: Table.api.formatter.operate,

                        }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },

        sellcards: function () {
            // 初始化表格参数配置
            Table.api.init({
            });
            // 初始化表格
            $('#table').bootstrapTable({
                url: location,
                columns: [
                    [

                        /* {field: 'id', title: __('Id')},*/
                        {field: 'username', title: __('用户名')},
                        /*{field: 'manystore_id', title: __('Manystore_id')},
                        {field: 'card_id', title: __('Card_id')},*/
                        {field: 'code', title: __('核销码')},

                        {field: 'status', title: '状态', table: table, formatter: function (value, row, index) {
                                console.log(value);
                                if(value==0)
                                {
                                    var value="待核销";
                                }
                                else if(value==1)
                                {
                                    var value="已核销";

                                } else if (value === 7) {
                                    value = "领取失败";
                                }


                                var custom = {已核销: 'success', 待核销: 'info'};
                                var color=custom[value];
                                var icon = 'fa fa-circle';


                                return  '<span class="text-' + color + '">' + (icon ? '<i class="' + icon + '"></i> ' : '') + value + '</span>';

                            }},

                        /*  {
                              field: 'image',
                              title: __('Image'),
                              operate: false,
                              events: Table.api.events.image,
                              formatter: Table.api.formatter.image
                          },*/

                        {
                            field: 'createtime',
                            title: __('领取时间'),
                            operate: 'RANGE',
                            addclass: 'datetimerange',
                            autocomplete: false,
                            formatter: Table.api.formatter.datetime
                        },


                    ]
                ]
            });


        },

        add: function () {
            Controller.api.bindevent();
            getType();
            getRedirect();

        },
        seller: function () {
            Controller.api.bindevent();
        },
        selluser:function(){
            Table.api.init({
            });
            // 初始化表格
            $('#table').bootstrapTable({
                url: location,
                columns: [
                    [

                        /* {field: 'id', title: __('Id')},*/
                        {field: 'username', title: __('用户名')},
                        {field: 'avatar', title: __('Avatar'), events: Table.api.events.image, formatter: Table.api.formatter.image, operate: false},
                        /*{field: 'manystore_id', title: __('Manystore_id')},
                        {field: 'card_id', title: __('Card_id')},*/
                        /*          {field: 'code', title: __('核销码')},*/



                        /*  {
                              field: 'image',
                              title: __('Image'),
                              operate: false,
                              events: Table.api.events.image,
                              formatter: Table.api.formatter.image
                          },*/

                        {
                            field: 'createtime',
                            title: __('参与时间'),
                            operate: 'RANGE',
                            addclass: 'datetimerange',
                            autocomplete: false,
                            formatter: Table.api.formatter.datetime
                        },


                    ]
                ]
            });

        },
        edit: function () {
            Controller.api.bindevent();
            getType();
            getRedirect();

        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;


});